package cn.herlioner.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import cn.herlioner.dao.StudentDAO;
import cn.herlioner.dao.TeacherDAO;
import cn.herlioner.dao.impl.TeacherDAOImpl;
import cn.herlioner.pojo.Student;
import cn.herlioner.pojo.Teacher;

public class TeacherAction extends ActionSupport implements SessionAware{
	Teacher teacher;
	TeacherDAO teacherDAO = new TeacherDAOImpl();
	List<Teacher> teachers ;
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	public TeacherDAO gettDAO() {
		return teacherDAO;
	}
	public void settDAO(TeacherDAO tDAO) {
		this.teacherDAO = tDAO;
	}
	public List<Teacher> getTeachers() {
		return teachers;
	}
	public void setTeachers(List<Teacher> teachers) {
		this.teachers = teachers;
	}
	/**'
	 * 查询老师信息
	 * @return
	 */
	public String list(){
		teachers = teacherDAO.list();
		ActionContext.getContext().getSession().put("teachers", teachers);
		return "list";
	}
	
	/**
	 * 添加老师信息
	 * @return
	 */
	public String auTeacher(){
		System.out.println(teacher.getTeaname());
		teacherDAO.add(teacher);
		/*
		 * �?��院系�?��判断
		 */
		teachers = teacherDAO.list();
		return "addSuccess";
		
	}
	
	/**
	 * 删除老师信息
	 */
	public String delete(){
		teacherDAO.delete(teacher.getTeanum());
		Map session = ActionContext.getContext().getSession();
		session.remove("teachers");
		teachers = teacherDAO.list();
		session.put("teachers", teachers);
		return "success";
	}
	/**
	 * 获取单个老师的信�?
	 * @return
	 */
	public String get(){
		System.out.println(teacher.getTeanum());
		teacher = teacherDAO.get(teacher.getTeanum());
		ActionContext.getContext().getSession().put("teacher", teacher);
		return "success";
	}
	/**
	 * 获取单个老师的信息
	 * @return
	 */
	public String getbyteacher(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
		Long teanum = (Long) session.get("Num");
		TeacherDAOImpl impl = new TeacherDAOImpl();
		Teacher teacher = impl.get(teanum);
		request.setAttribute("teacher", teacher);
		return "success";
	}
	/**
	 * 更新单个老师的信�?
	 * 用request中的新的密码值去代替session中的teacher并更新session中的teachers�?
	 */
	public String updateteacher(){
		Map session = ActionContext.getContext().getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
		Long teanum = (Long)session.get("Num");
		String teapad = teacher.getTeapad();
		Teacher teacher2 = teacherDAO.get(teanum);
		teacher2.setTeapad(teapad);
		teacherDAO.update(teacher2);
		teachers = teacherDAO.list();
		ActionContext.getContext().getSession().put("teachers", teachers);
		request.setAttribute("teacher", teacher2);
		return "success";
	}
	/**
	 * 教师信息添加
	 */
	public String addteacher(){
		teacherDAO.add(teacher);
		Map session = ActionContext.getContext().getSession();
		session.remove("teachers");
		teachers = teacherDAO.list();
		session.put("teachers", teachers);
		return "success";
	}
	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		
	}

}
